

	var changeArray =  new Array();
	var isChange = false;






	function loaddata(){
		var allFlag = $("#allFlag").val();
		var pno = $("#pagenum").val();
		var month=$("#month").val();
		var bonus=$("#bonus").val();
		var dept_ID=$("#dept_ID").val()=="null"?null:$("#dept_ID").val();
		var score = $("#score").val();
		var emp_id = $("#emp_id").val();
		var url = pms_url + "wh/performance/getWHPerformance";
		var params = {month:month,bonus:bonus,score:score,emp_id:emp_id,pno:pno,allFlag:allFlag,dept_ID:dept_ID};
		var obj = load(url,params);	
		var wh = obj.performance;
		$("#performancelist").find("tbody").html("");
		$.each(wh, function(index, p) {
		var tr ="<tr role='row'>"+
		"<td>"+(index+1)+"</td>"+
		"<td>"+((p.department_name=="模一仓储课")?"M1W":(p.department_name=="成品仓储课")?"FGW":"RMW")+"</td>"+
		"<td>"+p.emp_id+"</td>"+ 
		"<td>"+p.emp_name+"</td>"+
		"<td>"+p.dailywork+"</td>"+
		"<td>"+p.attendance+"</td>"+
		"<td>"+p.discipline+"</td>"+
		"<td>"+p.safetyatwork+"</td>"+
		"<td>"+p.audit+"</td>"+
		"<td>"+p.workhandover+"</td>"+
		"<td>"+p.abnormal+"</td>"+
		"<td>"+((p.score==null)?"0":p.score)+"</td>"+
		"<td>"+((p.composite==null)?"0":p.composite)+"</td>"+
		"<td>"+((p.bonus==null)?"0":p.bonus)+
		"<td>"+((p.memo=="sys" || p.memo==null)?"":p.memo)+"</td></tr>";
		$("#performancelist").find("tbody").append(tr);
	 });
		
		//加载平均奖金
		
		var aurl = pms_url + "wh/performance/getWHBonus";
		var param = {month:month,dept_ID:dept_ID,bonus:bonus,score:score,emp_id:emp_id};
		$.ajax({  
	        type : "post",  
	        url : aurl,  
	        data : param,
	        dataType : "json", 
	        async : false,  //同步
	        
	        beforeSend: function () {  
    	        $("#btnSearch").attr({ disabled: "disabled" });
    	        MaskUtil.mask("正在加载中..."); 
    	        
    	    },  
    	    
    	    complete: function () {  
            	$("#btnSearch").removeAttr("disabled");  
            	MaskUtil.unmask();   
            },  
            
	        
	        success : function(data){  
	           data = eval(data);  
		   		if (data.err_msg=="success") {
		   			$("#average").html(data.result.averagebonus);
		   			$("#bonusPool").html(data.result.allbonus);
		   			$("#actualBonus").html(data.result.actualbonus);
				}
				else {
					alert("error");
				}
	        }  
	    });
		
	
	}



	
	
	
	
	function save(){
		
		if(isChange){
			
			
			var month=$("#month").val();
			var table = document.getElementById("performancelist");
			changeArray.sort();
			var newArray = new Array();
			for(var i = 0; i < changeArray.length; i++){
				if(i > 0){
					if(changeArray[i] != changeArray[i-1])
						newArray.push(changeArray[i]);
				}else{
					newArray.push(changeArray[i]);
				}
			}
			
//			var a = JSON.parse("{\"data\":[]}");
			var ary = new Array();
			$('table tr').each(function(i){
				 
				if($.inArray(i, newArray) != -1){
					var tablelist = new Object();
					tablelist.month = month;
					tablelist.employee_id = $(this).children("td:eq(2)").text();
					tablelist.employee_name = $(this).children("td:eq(3)").text();
					tablelist.dailywork = $(this).children("td:eq(4)").text();
					tablelist.discipline = $(this).children("td:eq(6)").text();
					tablelist.safetyatwork = $(this).children("td:eq(7)").text();
					tablelist.audit = $(this).children("td:eq(8)").text();
					tablelist.workhandover = $(this).children("td:eq(9)").text();
					tablelist.abnormal = $(this).children("td:eq(10)").text();
					tablelist.composite = $(this).children("td:eq(12)").text();
					tablelist.memo =  $(this).children("td:eq(14)").text();
					
					ary.push(tablelist);
					
				}
				
			});
			
//			var obj =JSON.stringify(a);
			
			var aurl = pms_url + "wh/performance/save";
			$.ajax({  
		        type : "post",  
		        url : aurl,  
		        data : {param:JSON.stringify(ary),month:month},
		        dataType : "json", 
		        async : false,  //同步
		        success : function(data){  
		           data = eval(data);  
			   		if (data.err_msg=="success") {
			   			if(data.result == "0"){
			   				alert("保存成功");
			   				loaddata();
			   			}else{
			   				alert("error!");
			   			}
					}else {
						alert("error!");
					}
		        }  
		    });
			
			
			
			
			//alert(table.rows[0].cells[2].innerHTML);
			
			/*$('table tr').each(function(trindex,tritem){
	        $(tritem).find("td").each(function(tdindex,tditem){
	       	 
	       	 if(tdindex = $(tritem).find("td").length-1){
	       		 a.data.add(tablelist);
	       	 }
	        });
	    });
		*/
			
		
		}else{
			alert("内容无改动，无需保存！");
			return;
		}
		
		isChange = false;
		
	}
	



$(function () { 
	loaddata();
	
	//查询
	$('#btnSearch').on('click', function () {
		$("#pagenum").val(1);
		loaddata();
		
	});
	
	//重置
	$("#btnReset").on('click', function () {
		$("#searchform input").val("");
		$("#searchform select").val("");
	});	

	//更新
	$("#btnSyschron").on('click', function () {
		update();
		loaddata();
	});	
	
	
	//导出
	$("#btnExport").on('click', function () {
		$("#performancelist").tableExport({
		    headings: true, 
		    footers: true, 
		    formats: ["xls"],
		    fileName: "data",
		    bootstrap: true,
		    position: "bottom",
		    ignoreRows: null,
		    ignoreCols: null
		});   

	});	
	
	
	//保存
	$("#btnSave").on('click', function () {
		save();
	});	
	
	
	
	$(document).on("click","td",function(){
		
		if($(this).children("input").length > 0) 
			   return false;
		
		var td = $(this);
		
		var trindex = td.parents("tr").index() + 1;
		
		changeArray.push(trindex);
		
		var tdindex = td.parents("tr").find("td").index($(this));
		if(tdindex > 3 && tdindex != 5 && tdindex != 11 && tdindex != 13){
			var td = $(this);  
		    //保存初始值  
		    var tdText = $(this).text();  
		    //给td设置宽度和给input设置宽度并赋值  
		    var input = $("<input type='text' />");
		    td.text("");
		    input 
			   .width(td.width())                   //设置文本框宽度与td相同 
			   .height(td.height()) 
			   .val(tdText) 
			   .appendTo(td)                         //把创建的文本框插入到tdObj子节点的最后
			   .trigger("focus")                        //用trigger方法触发事件 
			   .trigger("select"); 
		    
		    //失去焦点的时候重新赋值  
		    var inputDom = $(this).find("input");  
		    inputDom.blur(function(){  
		    		
		    	var error = false;
		    	
		    	var newText = $(this).val();  
		    	
		    	if (isNaN(newText) && tdindex != 14){   //用于检查其参数是否是非数字值  
		            alert("必须输入数字！");
		            td.html(tdText); 
		            return;
	            }
		    	
		    	if(tdindex == 4){
		    		if(newText > 20 || newText < 0){
		    			alert("此最低分0分,最高分20分");
		    			error = true;
		    		}
		    	}else if(tdindex == 6){
		    		if(newText > 15 || newText < 0){
		    			alert("此最低分0分,最高分15分");
		    			error = true;
		    		}
		    	}else if(tdindex == 7){
		    		if(newText > 10 || newText < 0){
		    			alert("此最低分0分,最高分10分");
		    			error = true;
		    		}
		    	}else if(tdindex == 8 || tdindex == 9 || tdindex == 10){
		    		if(newText > 5 || newText < 0){
		    			alert("此最低分0分,最高分5分");
		    			error = true;
		    		}
		    	}else if(tdindex == 12){
		    		if(newText > 125 || newText < -125){
		    			alert("此最低分-125分,最高分125分");
		    			error = true;
		    		}
		    	}
		    	
		       
		        $(this).remove();  
		        
		        if(error){
		        	td.text(tdText);
		        }else{
		        	td.text(newText);
		        }
		        
		        if(newText != tdText){
			    	isChange = true;
			    }
		        
		    });
		}
	   
	    
	    
	});
	
	
	
	
});